package com.wl.user.controller;

import com.wl.basic.dto.SmsCodeDto;
import com.wl.basic.exception.BusinessException;
import com.wl.basic.util.AjaxResult;
import com.wl.user.domain.Logininfo;
import com.wl.user.dto.LoginDto;
import com.wl.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;


    /**
     * 前台登录和后台登录接口
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
           Map<String,Object> map = logininfoService.account(loginDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"登录失败");
        }
    }

    /**
     * 微信登录接口
     * @param
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody LoginDto loginDto){
        try {
            return logininfoService.wechatLogin(loginDto.getCode());
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }

    /**
     * 微信绑定接口
     * @param
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody Map<String,String>result){
        try {
            return logininfoService.wechatBinder(result);
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常，请稍后重试!!!");
        }
    }
}
